﻿using System;
using System.Diagnostics;
using System.Net;

namespace IllustratedCsharp7_21
{

    class MyDownloadString
    {
        Stopwatch sw = new Stopwatch();

       public void DoRun()
       {
        const int LargeNumber = 6_000_000;
        sw.Start();
            int t1 = CountCharacters(1, "http://www.baidu.com");
            int t2 = CountCharacters(2, "http://www.so.com");
            CountToLargeNumber(1, LargeNumber);
            CountToLargeNumber(2, LargeNumber);
            CountToLargeNumber(3, LargeNumber);
            CountToLargeNumber(4, LargeNumber);
            Console.WriteLine($"Chars in http://www.baidu.com        :{ t1 }");
            Console.WriteLine($"Chars in http://www.so.com           :{ t2 }");
        }

        private int CountCharacters(int id, string uriString) {
            WebClient wc1 = new WebClient();
            Console.WriteLine("Starting call{0}     :    {1,4:N0} ms",
                                id,sw.Elapsed.TotalMilliseconds);
            string result = wc1.DownloadString(new Uri(uriString));
            Console.WriteLine("  Call{0} completed  :    {1,4:N0} ms",
                               id, sw.Elapsed.TotalMilliseconds);

            return result.Length;
        }

        private void CountToLargeNumber(int id, int value)
        {
            for (long i = 0; i < value; i++) ;
            Console.WriteLine("  End count{0} :  {1,4:N0} ms",
                    id, sw.ElapsedMilliseconds);
        }
        
    }
    class Program
    {
        static void _Main(string[] args)
        {
            MyDownloadString ds = new MyDownloadString();
            ds.DoRun();
        }
    }
}
